import Taro from '@tarojs/taro'
import app from '../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            doctor: {
                name: '',
                head_image: '',
                positional_title: '',
            },
            advertisements: {
                offset: 0,
                length: 25,
                items: [{
                    id: '',
                    position_id: '',
                    image: '',
                    title: '',
                    url: '',
                    sort: '',
                }]
            },
            noread:''   //未读条数
        };
        return this.state;
    },
    getDoctor() {
        var me = this;
        var data = {id: me.state.doctor.id};
        var flag = true;
        if(me.beforeGetDoctor) {
            flag = me.beforeGetDoctor(data);
        }
        if(flag) {
            app.get("web/doctor/get", data, function(res) {
                me.setState({doctor: res.data.result});
                if(me.afterGetDoctor) {
                    me.afterGetDoctor(res);
                }
            });
        }
    },
    allAdvertisement() {
        var me = this;
        var data = {code: '100'};
        var flag = true;
        if(me.beforeAllAdvertisement) {
            flag = me.beforeAllAdvertisement(data);
        }
        if(flag) {
            app.get("web/advertisement/banners", data, function(res) {
                var data = [];
                for(var i = 0; i < res.data.result.length; i++) {
                    var m = {
                        id: res.data.result[i].id,
                        position_id: res.data.result[i].position_id,
                        image: app.getPath(res.data.result[i].image),
                        title: res.data.result[i].title,
                        url: res.data.result[i].url,
                        sort: res.data.result[i].sort,
                    }
                    data.push(m);
                }
                me.setState({advertisements: {items: data}});
                if(me.afterAllAdvertisement) {
                    me.afterAllAdvertisement(res);
                }
            });
        }
    },
    componentWillMount () {
       
    },
    navigateTo2(url) {
        let reg = new RegExp(/^(http|https):/);
        let bool = url.match(reg);
        if (bool != null && bool instanceof Object ) {
            app.navigateTo('../webViewContaziner/webViewContaziner'+ `?webUrl=${url}`)
        } else {
          app.toast('不是合法链接')
          app.navigateTo('../webViewContaziner/webViewContaziner' + `?webUrl=${url}`)
        }
    },
    navigateTo(url){
        app.navigateTo(url)
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = v;
        this.setState({n: temp[names[i]]});
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    onShareAppMessage(res) {
        return {
            title: '首页',
            path: '/pages/doctorpages/homepage/homepage'
        }
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
